import logging
from logging.handlers import SMTPHandler
from logging import config
import json
from Email import Email

class Logger:
    def __init__(self,fileurl):
        # raise IOError('Test')
        file_object = open(fileurl,"r")
        try:
            configJson = file_object.read()
            confDict =json.loads(configJson)
            logging.config.dictConfig(confDict)
            self.fileInfoLogger = logging.getLogger()
        except Exception as e:
            emailController = Email.EmailController(loggerConfigPath=fileurl)
            emailController.sendEmail(msgStr="构建Logger对象时出错！",subject="紧急情况！\n %s" %(e),withLog=False)
        finally:
            file_object.close()

    def main(self):
        logger = self.fileInfoLogger
        logger.info("Test info")

if __name__ == "__main__":
    log = Logger("./logging.json")
    log.main()
